import {
	objDeepCopy,
	getCascaderObj,
	getDateString
} from "@/assets/js/common.js";
let Controller = {
	created() {
		this.init();
	},
	mounted() {

	},
	methods: {
		init() {

		},
		//根据调拨单号查询
		getMsgAjax(that, number) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/supplier/whWarehouse/getEntity',
				// url:'/api/whWarehouse/getEntity',
				data: {
					number: number
				}
			}).then((res) => {
				console.log(res);
				// that.Model.getTypeOptionData = [];
				var data = res.data;
				if (data.status == 200) {
					var obj = {};
					var result = data.result;
					if (result) {
						this.Model.messageShow = true;
						/* tabFormInline */
						let inWare = result.inWarehouseInfo.theNameOfThePlace + '-' + result.inWarehouseInfo.descriptionOfWarehouseLocation;
						let outWare = result.outWarehouseInfo.theNameOfThePlace + '-' + result.outWarehouseInfo.descriptionOfWarehouseLocation;
						let ortype;
						if (result.typeDict) {
							ortype = result.typeDict.value
						}
						that.Model.tabFormInline = [{
								label: '发货仓库',
								value: outWare
							},
							{
								label: '收货仓库',
								value: inWare
							},
							{
								label: '调拨单类型',
								value: ortype
							}
						]

					} else {
						that.tips(that, 'warning', '未查询到相关调拨单号');
						this.Model.messageShow = false;
					}
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},

		//搜索 更多
		searchMore() {
			var no = this.Model.number;
			this.getMsgAjax(this, no)
		},
		//扫码入库 请求
		codeEntryAjax(that, code) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url: apiTest + '/supplier/whWarehouseDetail/getGoodsInfo?batchNo=' + code,
				// url:'/api/whWarehouseDetail/getGoodsInfo?batchNo='+code,
			}).then((res) => {
				var data = res.data;
				if (data.status == 200) {
					var obj = {};
					var result = data.result;
					if (result) {
						obj = objDeepCopy(result);
						obj.mainQuantity = 1;
						if (result.batchNo) {
							obj.isBatchNo = true;
						} else {
							obj.isBatchNo = false;
						}
						that.Model.tableData.push(obj)
					} else {
						this.tips(this, 'warning', '未找到相关条码');
					}
				} else {

				}

			});
		},

		//扫码入库
		codeEntry() {
			var code = this.Model.searchCode;
			if (code.length == 0) {
				this.tips(this, 'warning', '请输入条码或者商品编码');
				return;
			}
			var tableData = this.Model.tableData;
			var codeIndexOf = this.valueInData(code, tableData);
			if (codeIndexOf == -1) {
				this.codeEntryAjax(this, code);
			} else {
				this.tips(this, 'warning', '条码已存在')
			}
		},
		//遍历 判断是否存在数组中
		valueInData(val, data) {
			var indexof = -1;
			data.forEach(function(value, index, array) {
				Object.keys(value).forEach(function(key) {
					// console.log(key,value[key]);
					if (key == 'batchNo' && value[key] == val) {
						indexof = 0;
						return;
					}
				});
			})
			return indexof;
		},
		//删除 表格数据
		deleteTableData() {
			var that = this;
			var TableSelectData = this.Model.TableSelectData;
			var tableData = this.Model.tableData;
			if (TableSelectData.length == 0) {
				this.tips(this, 'warning', '请先勾选需要删除的数据');
				return;
			}
			this.$confirm('此操作将删除相应数据, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				TableSelectData.forEach(function(value, index, array) {
					tableData.forEach(function(val, idx, arr) {
						if (value == val) {
							that.Model.tableData.splice(idx, 1);
						}
					})
				})
				this.$message({
					type: 'success',
					message: '删除成功!'
				});
			}).catch(() => {

			});
		},
		//返回上一页
		back() {
			this.$router.push({
				path: 'insideAllocationStore',
				query: {

				}
			});
		},
		//点击 提交调拨单
		onConfirmNew() {
			var arr = [];
			var obj = {};
			var tableData = this.Model.tableData;
			for (let i = 0; i < tableData.length; i++) {
				obj = {
					sourceDetailsId: tableData[i].id,
					barCode: tableData[i].batchNo,
					mainQuantity: 1
				}
				arr.push(obj);
			}
			if (tableData.length == 0) {
				this.tips(this, 'warning', '请选择数据');
				return;
			}
			//提交的数据 formInline
			var formInline = this.Model.formInline;
			var number = this.Model.number;
			var params = {
				orNumber: number,
				remarks: formInline.remarks,
				transferType: 20,
				wDetails: arr
			}
			formInline.goodsIds = arr;
			this.$confirm('此操作将提交新建订单, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				this.confirmNewAjax(this, params);
			}).catch(() => {

			});

		},
		//提交调拨单 请求
		confirmNewAjax(that, params) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/supplier/whWarehouseDetail/operatingOutAndIn',
				// url:'/api/whWarehouseDetail/operatingOutAndIn',
				data: params
			}).then((res) => {
				var data = res.data;
				if (data.status == 200) {
					var result = data.result;
					if (result == 1) {
						that.tips(that, 'success', '操作成功');
						that.back();
					}
				} else {
					that.tips(that, 'warning', data.msg)
				}

			});
		},
		//点击导入物料按钮
		importMaterial() {
			this.Model.importFileDialog = true;
		},
		//选择文件
		fileChange() {
			var fileObj = document.getElementById("fileup").files[0];
			var form = new FormData();
			form.append("file", fileObj);
			this.Model.upFileForm = form;
			var that = this;
			var obj = document.getElementById("fileup");
			var len = obj.files.length;
			for (var i = 0; i < len; i++) {
				var temp = obj.files[i].name;
				that.Model.fileupName = temp;
			}
		},
		//点击上传
		uploadExcel() {
			var apiTest = this.apiTest;
			var tableData = objDeepCopy(this.Model.tableData);
			var that = this;
			this.$ajax({
				method: 'post',
				url: apiTest + '/supplier/whWarehouseDetail/importExcelList',
				// url:'/api/whWarehouseDetail/importExcelList',
				data: that.Model.upFileForm
			}).then((res) => {
				console.log(res);
				var obj = {};
				if (res.data.status == 200) {
					var result = res.data.result;
					if (result) {
						for (let i = 0; i < result.length; i++) {
							var indexof = -1;
							obj = objDeepCopy(result[i]);
							if (tableData.length == 0) {
								that.Model.tableData.push(obj);
							} else {
								tableData.forEach(function(value, index, array) {
									if (value.batchNo == obj.batchNo) {
										indexof = 0;
									}
								})
								if (indexof == -1) {
									that.Model.tableData.push(obj);
								}
							}

						}
					}
					// that.tips(that,'success','导入成功！')
					setTimeout(function() {
						that.Model.importFileDialog = false;
					}, 1000);
				} else {
					that.tips(that, 'warning', '导入失败！')
				}
			});
		},
		//下载模板
		downloadExcel() {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url: apiTest + '/supplier/whWarehouseDetail/getGoodsExcelInfo',
				// url:'/api/whWarehouseDetail/getGoodsExcelInfo',
			}).then((res) => {
				console.log(res);
				if (res.data.status == 200) {
					var result = res.data.result;
					if (result.fileUrl) {
						window.open(result.fileUrl);
					}

				} else {

				}
			});
		},
		//取消订单
		cancelOrder() {
			this.$router.push({
				path: 'listAllocationStore',
				query: {

				}
			});
		},
		showNotSureBarCodeP1() {

		},
		showNotSureBarCodeP2() {

		},
		toSearchNotSureBarCode() {

		},
		//
		selectedHighlight(data) {
			// 如果选中则添加高亮样式
			if (this.Model.TableSelectData.indexOf(data.row) > -1) {
				return {
					"background-color": "#FEF6EE"
				};
				return {}
			}
		},
		tips(that, type, message) {
			let duration;
			type == 'success' ? duration = 3000 : duration = 10000;
			that.$message({
				message: message,
				type: type,
				duration: duration
			});
		},
		toggleTableDetail(val) {
			const obj = this.Model[val];
			if (obj.flag === true) {
				this.Model[val].src = require('@/assets/img/icon_collapse.png');
				this.Model[val].flag = false;
			} else {
				this.Model[val].src = require('@/assets/img/icon_unfold.png');
				this.Model[val].flag = true;
			}
		},
		handleSelectionChange(val) {
			this.Model.TableSelectData = val;
		},
		handleCurrentChange(val) {
			this.Model.pager.params.pageIndex = val;
		},
		handleSizeChange(val) {
			this.Model.pager.params.pageSize = val;
		},
		cardClick(color) {
			this.Model.cardSelected = color;
		},
		showChild(val) {
			this.Model.searchFromData = val;
		},
		toSearch(val) {
			var params = {
				conditcion: this.Model.searchFromData
			}
			this.getCommodity(this, params)
		},
		toClear(val) {
			this.Model.searchFromData = this.Model.defaultSearchFromData;
		},
		toExport(val) {
			console.log(val);
		},
		//合计
		getSummaries(param) {
			var that = this;
			const {
				columns,
				data
			} = param;
			const sums = [];
			columns.forEach((column, index) => {
				if (index === 0) {
					sums[index] = '总价';
					return;
				} else if (index === 5 || index === 6) {
					const values = data.map(item => Number(item[column.property]));
					sums[index] = values.reduce((prev, curr) => {
						const value = Number(curr);
						if (!isNaN(value)) {
							return prev + curr;
						} else {
							return prev;
						}
					}, 0);
					if (index === 6) {
						that.Model.totlePrice = sums[index];
					} else {

					}

				} else {
					sums[index] = '/';
				}
			});

			return sums;
		}
	}
}
export default Controller;
